var ns,toast,app,WXPhotoPicker;
app = new Vue({
    el: '#app',
    data: {
        form: {
            TaskID:0,
            Sort:0,
            jieduanstatus:0,
            CheckName:'',
            CheckContent:''
        },
        JcList:[],
        state:0,
        Content:'',
        status:1,
        isnext:false,
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
        IsRoomOpen:false,
        ID:0,
        type:1,//1-巡查结果，2-临时巡查,3-查验结果
        filelist:[],
    },
    methods: {
        getdata:function(){
            var that=this;
            that.state=0;
            that.status=1;
            var options = {};
            options.ID = that.ID;
            if(that.type==2){
                options.action = "gettemporaryinspectionresults";
            }else{
                options.action = "gteinspectionresults";
            }
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form.TaskID=data.TaskID;
                    that.form.Sort=data.Sort;
                    that.form.jieduanstatus=data.Status;
                    that.form.CheckName=data.CheckName;
                    that.form.CheckContent=data.CheckContent;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                api.sendEvent({
                    name:'is_hidden_right_xcResult',
                    extra: {
                        keyId: "right_renwu",
                        IsHidden: that.type
                    }
                })
            })

        },
        //选择装修阶段
        do_chooseSteps:function(){
            var that=this;
            that.IsRoomOpen=true;
            that.JcList=[];
            var options = {};
            options.ID=that.form.TaskID;
            options.action = "getdecorationprocess";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.JcList=data.list;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        select_all_active:function(state){
            var that=this;
            that.state=state;
        },
        do_agree:function(){
            var that=this;
            that.isnext=!that.isnext;
            if(that.isnext==true){
                that.status=2
            }else{
                that.status=1
            }
        },
        doCloseRoom:function(){
            var that=this;
            that.IsRoomOpen=false;

        },
        updatetime:function(datetime){
            return ns.SetTime_style(datetime);
        },

        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.do_imageFilter(ret.data);
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.do_imageFilter(item.path);
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        //图片压缩
        do_imageFilter:function(path){
            var that=this;
            var imgName = Date.now() + '.jpg';
            var imgFullName = 'fs://photo/' + imgName;
            var imageFilter = api.require('imageFilter');
            imageFilter.compress({
                img: path,
                isClarityimg:false,
                quality: 0.2,
                scale:0.5,
                save: {
                    album: true,
                    imgPath: 'fs://photo/',
                    imgName: imgName
                },
            },function( ret, err ){      
                if( ret.status ){
                    that.filelist.push(imgFullName);
                }else{
                    alert( JSON.stringify( err ) );
                }
            });
        },
        remove_img: function(index) {
            var that = this;
            that.imglist.splice(index, 1);
            that.filelist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        do_save: function () {
            var that = this;
            if (that.state!=1&&that.state!=2) {
                api.toast({
                    msg: '请选择巡查结果正常与否',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            
            // var filelist = [];
            // for (var i = 0; i < that.imglist.length; i++) {
            //     filelist.push(that.imglist[i].url);
            // }
            var options = {};
            options.ID = that.ID;
            options.state = that.state;
            options.Content = that.Content;
            options.status = that.status;
            options.Type = that.type;
            options.action = "savainspectionresults";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.Content = '';
                    that.imglist = [];
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'xuncha_updata',
                    });
                    
                    setTimeout(function(){
                        api.closeWin();
                    }, 500);

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: that.filelist
                }
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    toast = new auiToast();
    ns = window.Foresight.Util;
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.ID || 0;
    app.type = api.pageParam.type || 1;
    app.getdata();
    api.addEventListener({
        name: 'do_renwu_zhuanjiao'
    }, function (ret, err) {
        ns.openWin('chooseperson_frm', '选择',{
            source: 'chooseuser',
            ID:app.ID,
            zxtype:1
        });
    });
    api.addEventListener({
        name: 'zhuanjiao_updata'
    }, function (ret, err) {
        api.closeWin();
    });
    
};
